package com.app.twth.ui.putaway;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemPutawayDetailListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;


public class PutawayDetailAdapter extends RecyclerView.Adapter<PutawayDetailAdapter.MyViewHolder> {
    private LayoutInflater mInflater;
    private List<PutawayDetails> data = new ArrayList<>();
    private MyOnClickListener locationListener;

    public PutawayDetailAdapter(Context mContext, List<PutawayDetails> data, MyOnClickListener locationListener) {
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.locationListener = locationListener;
    }

    public void setData(List<PutawayDetails> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemPutawayDetailListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemPutawayDetailListBinding itemBinding;

        public MyViewHolder(ItemPutawayDetailListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            PutawayDetails lpnSkus = data.get(position);
            List<PutawayDetails.SkuListDTO> skuList = lpnSkus.getSkuList();

            if (Utils.isEmpty(lpnSkus.getLocationCode())){
                if ((skuList!=null)&&(skuList.size()>0)){
                    itemBinding.tvItem.setText(lpnSkus.getIbLpn() + " ("+skuList.get(0).getSku()+") ");
                }else {
                    itemBinding.tvItem.setText(lpnSkus.getIbLpn() + " (--) ");
                }
            }else {
                if ((skuList!=null)&&(skuList.size()>0)){
                    itemBinding.tvItem.setText(lpnSkus.getIbLpn() + " ("+skuList.get(0).getSku()+") "+lpnSkus.getLocationCode());
                }else {
                    itemBinding.tvItem.setText(lpnSkus.getIbLpn() + " (--) "+lpnSkus.getLocationCode());
                }
            }
            itemBinding.layout.setOnClickListener(v -> {
                locationListener.OnClick(position);
            });
        }
    }

}
